﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity;
using BLL;


namespace BookStore.Controllers
{
    public class PayController : Controller
    {
        //
        // GET: /Pay/pay
        //订单页面
        static List<CartItem> carfo;
        static List<OrderDetails> odet;
        static OrderInformatica ordin;
        public void Addorder()
        {
            ordin.OrderId =DateTime.Now.GetHashCode()+ DateTime.Now.Millisecond.ToString() +Convert.ToString(Request.Cookies["User"].Values["UserId"]);
            ordin.OrderDate = DateTime.Now;
            ordin.OrderState = 1;
            ordin.UserId = Convert.ToInt32(Request.Cookies["User"].Values["UserId"]);
        }

        public ActionResult pay()
        {
            ViewBag.Goods = carfo;
            ViewBag.money = ordin.OrderAmount;
            return View();
        }

        public ActionResult pay1(string cid)
        {
            decimal total = 1;
            string ass = cid;
            string[] abc = ass.Split(',');
            carfo = T_BOrder.OrderInfo(abc);
            odet = new List<OrderDetails>();
            ordin = new OrderInformatica();
            Addorder();
            foreach (CartItem ct in carfo)
            {
                OrderDetails od = new OrderDetails();
                od.OrderId = ordin.OrderId;
                od.BookId = ct.BookId;
                od.BookPrice = ct.Price;
                od.Count = ct.Num;
                total += ct.Price * ct.Num;
                odet.Add(od);
            }
            ordin.OrderAmount = total;
            ViewBag.Goods = carfo;
            ViewBag.money = ordin.OrderAmount;
            return Redirect("/pay/pay");
        }


        //获取省份信息
        public JsonResult GetProvincelist()
        {
            List<Area> save = T_BOrder.Save();//获取省份      
            return Json(save, JsonRequestBehavior.AllowGet);
        }
        //根据省份ID获取城市
        public JsonResult GetCitylist(string pid)
        {
            List<Area> City = T_BOrder.City(pid);//获取省份
            return Json(City, JsonRequestBehavior.AllowGet);
        }
        //获取区县
        public JsonResult GetCountylist(string cid)
        {
            List<Area> County = T_BOrder.County(cid);//获取省份
            return Json(County, JsonRequestBehavior.AllowGet);
        }
        //新增地址
        [HttpPost]
        public JsonResult pay(ShoppingAddress sad)
        {
            //sad.UserID = Convert.ToInt32(Request.Cookies["User"].Values["UserId"]);
            sad.UserID = 2;
            T_BOrder.AddAddress(sad);
            return Json("", JsonRequestBehavior.AllowGet);

            //return Content("<script>alert('添加成功！');history.go(-1);;</script>");
        }
        //获取地址信息
        public JsonResult Address()
        {
            //int id = Convert.ToInt32(Request.Cookies["User"].Values["UserId"]);
            int id = 2;
            List<ShoppingAddress> adr = T_BOrder.Address(id);//获取省份      
            return Json(adr, JsonRequestBehavior.AllowGet);
        }

        //查询默认地址
        public JsonResult Addressm()
        {
            //int id = Convert.ToInt32(Request.Cookies["User"].Values["UserId"]);
            int id = 2;
            List<ShoppingAddress> adr = T_BOrder.Cham(id);//获取省份      
            return Json(adr, JsonRequestBehavior.AllowGet);
        }

        //修改地址信息
        public JsonResult Ucha(int id)
        {
            List<ShoppingAddress> adr = T_BOrder.Ucha(id);
            return Json(adr, JsonRequestBehavior.AllowGet);
        }
        //修改地址
        [HttpPost]
        public JsonResult Upcha(ShoppingAddress sad)
        {
            T_BOrder.UpdataAddress(sad);
            //return Content("<script>alert('修改成功！');window.location.href='/pay/pay';</script>");
            return Json("", JsonRequestBehavior.AllowGet);

        }

        //删除地址
        public JsonResult DelAddress(int id)
        {
            T_BOrder.DelAddress(id);
            //return Content("<script>window.location.href='/pay/pay';</script>");
            return Json("", JsonRequestBehavior.AllowGet);

        }
        //设置默认地址
        public ActionResult mAddress(int id)
        {
            T_BOrder.Addressm(id);
            return Content("<script>history.go(-1);alert('修改成功！');</script>");
            //return Json("", JsonRequestBehavior.AllowGet);
        }

        //获取购物详情信息
        public ActionResult paying(int id)
        {
            ordin.OrderAddress = id;
            if (T_BOrder.AddOrder(odet, ordin))
            {
                return Content("<script>alert('支付成功！');window.location.href='/Success/Index';</script>");
            }
            else
            {
                return Content("<script>alert('支付失败！');history.go(-1);;</script>");
            }
        }

    }
}
